package crypto;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;
import java.io.*;

/**
 * <p>Title: rsa</p>
 * <p>Description: RSA Cryptography Algorithm</p>
 * @authors Dave Mudgett + YOUR TEAM!!
 * @version 1.0
 */

public class rsa_ui extends JFrame {
  private JPanel contentPane;
  private JLabel jLabel1 = new JLabel();
  private JLabel jLabel2 = new JLabel();
  private JTextArea jTextArea1 = new JTextArea();
  private JButton jButton1 = new JButton();
  private JButton jButton2 = new JButton();
  public static BigInteger PQ;
  public static BigInteger E;
  public static BigInteger D;
  public static BigInteger T;
  public static BigInteger C;
  JTextArea jTextArea2 = new JTextArea();
  JLabel jLabel3 = new JLabel();
  JButton jButton3 = new JButton();
  JTextArea jTextArea3 = new JTextArea();
  JLabel jLabel4 = new JLabel();
  JTextArea jTextArea4 = new JTextArea();
  JTextArea jTextArea5 = new JTextArea();
  JLabel jLabel5 = new JLabel();
  JLabel jLabel6 = new JLabel();
  JButton jButton4 = new JButton();
  JButton jButton5 = new JButton();
  JButton jButton6 = new JButton();

  //Construct the frame
  public rsa_ui() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    //setIconImage(Toolkit.getDefaultToolkit().createImage(gcd_ui.class.getResource("[Your Icon]")));
    contentPane = (JPanel) this.getContentPane();
    jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
    jLabel1.setText("RSA Crytography Applet");
    jLabel1.setBounds(new Rectangle(68, 12, 259, 17));
    contentPane.setLayout(null);
    this.setSize(new Dimension(472, 400));
    this.setTitle("RSA Cryptography");
    jLabel2.setText("Cleartext:");
    jLabel2.setBounds(new Rectangle(10, 29, 60, 17));
    jTextArea1.setBounds(new Rectangle(4, 50, 452, 50));
    jButton1.setBounds(new Rectangle(69, 187, 78, 27));
    jButton1.setText("Encrypt");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    jButton2.setBounds(new Rectangle(189, 187, 85, 27));
    jButton2.setText("Decrypt");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton2_actionPerformed(e);
      }
    });
    jTextArea2.setText("");
    jTextArea2.setBounds(new Rectangle(4, 125, 452, 50));
    jLabel3.setText("Ciphertext:");
    jLabel3.setBounds(new Rectangle(10, 108, 53, 15));
    jButton3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton3_actionPerformed(e);
      }
    });
    jButton3.setText("Clear");
    jButton3.setBounds(new Rectangle(315, 187, 85, 27));
    jTextArea3.setText("");
    jTextArea3.setBounds(new Rectangle(30, 243, 225, 17));
    jLabel4.setText("PQ");
    jLabel4.setBounds(new Rectangle(9, 242, 34, 15));
    jTextArea4.setBounds(new Rectangle(30, 276, 225, 17));
    jTextArea4.setText("");
    jTextArea5.setBounds(new Rectangle(30, 309, 225, 17));
    jTextArea5.setText("");
    jLabel5.setBounds(new Rectangle(9, 277, 34, 15));
    jLabel5.setText("E");
    jLabel6.setBounds(new Rectangle(9, 312, 34, 15));
    jLabel6.setText("D");
    jButton4.setBounds(new Rectangle(303, 230, 112, 24));
    jButton4.setText("Generate Keys");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton4_actionPerformed(e);
      }
    });
    jButton5.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton5_actionPerformed(e);
      }
    });
    jButton5.setText("Store Keys");
    jButton5.setBounds(new Rectangle(303, 275, 112, 24));
    jButton6.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton6_actionPerformed(e);
      }
    });
    jButton6.setText("Read Keys");
    jButton6.setBounds(new Rectangle(303, 317, 112, 24));
    contentPane.setPreferredSize(new Dimension(472, 400));
    contentPane.add(jLabel1, null);
    contentPane.add(jLabel2, null);
    contentPane.add(jTextArea1, null);
    contentPane.add(jLabel3, null);
    contentPane.add(jTextArea2, null);
    contentPane.add(jButton3, null);
    contentPane.add(jButton1, null);
    contentPane.add(jButton2, null);
    contentPane.add(jButton4, null);
    contentPane.add(jButton5, null);
    contentPane.add(jButton6, null);
    contentPane.add(jLabel6, null);
    contentPane.add(jLabel4, null);
    contentPane.add(jLabel5, null);
    contentPane.add(jTextArea3, null);
    contentPane.add(jTextArea4, null);
    contentPane.add(jTextArea5, null);
  }
  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

  void jButton1_actionPerformed(ActionEvent e) {

  // jButton1 is the "Encrypt" button.
  // Get text from JTextArea1 - this is cleartext.
    String clearText = jTextArea1.getText();

  // Now you need to do everything associated with encrypting.
  
  // You first need to convert the text to numbers. The most
  // common way to do this is to use the String getBytes() method,
  // which converts the String into a byte array in the platform's
  // default character set (ASCII). Then use one of the BigInteger 
  // constructors to create a BigInteger from this byte array.
  
  // But you could also make up your own function to convert to numbers.
  
  }

  void jButton2_actionPerformed(ActionEvent e) {

  //jButton2 is the "Decrypt" button.
  // Get text from JTextField2 - this is ciphertext
    String ciphertext = jTextArea2.getText();

   // Here you need to do everything else associated with decrypting.
  }
  void jButton3_actionPerformed(ActionEvent e) {

   // jButton3 is the "Clear" button.
   // This code clears all text areas.
    jTextArea1.setText("");
    jTextArea2.setText("");
    jTextArea3.setText("");
    jTextArea4.setText("");
    jTextArea5.setText("");

}
  void jButton4_actionPerformed(ActionEvent e) {

   // jButton4 is the "Generate Key" button.
   
   // First, generate your key, PQ, E, and D.
	  //NEED TO MODIFY THIS
	PQ = BigInteger.valueOf(12345678);
	E = BigInteger.valueOf(12345678);
	D = BigInteger.valueOf(12345678);
	  
	  
   // This code writes the key out to the text areas.
    jTextArea3.setText(PQ.toString());
    jTextArea4.setText(E.toString());
    jTextArea5.setText(D.toString());

  }
  void jButton5_actionPerformed(ActionEvent e) {

  // jButton5 is the "Store Key" button.
  // Here is where you need to put code to write keys to text file.

  try {

  FileWriter output = new FileWriter("keys.txt");
  
  // You need to add the code to write PQ, E, and D on separate lines
  // of the text file.
}
catch(FileNotFoundException e51) {
}
catch(IOException e52) {
}

  }
  void jButton6_actionPerformed(ActionEvent e) {

   // This code reads keys from text areas. This is for
   // testing things out. Uncomment this code, and comment
   // out the file reader code, if and only you want to use it.
   // Note the try, catch, finally block, which makes sure
   // these are integers.

//   try {

//     BigInteger PQ1 = new BigInteger(jTextArea3.getText());
//     BigInteger E1  = new BigInteger(jTextArea4.getText());
//     BigInteger D1  = new BigInteger(jTextArea5.getText());
//     PQ = PQ1;
//     E = E1;
//     D = D1;

//   } catch (NumberFormatException e61){
//       JOptionPane.showMessageDialog(this,
//       "The Input Must Be An Integer. Please Reenter.");
//   } finally {
//       repaint();
//   }

   try {

     BufferedReader input = new BufferedReader(new FileReader("keys.txt"));
     
    // I've done this part for you, but you will need to do the
    // writer code (it's just a few lines).
    
    // Note that this assumes each variable is on a new line.
     BigInteger PQ1 = new BigInteger(input.readLine());
     BigInteger E1 =  new BigInteger(input.readLine());
     BigInteger D1 =  new BigInteger(input.readLine());
     
	 PQ = PQ1;
     E = E1;
     D = D1;
     input.close();
     
   // This code writes the key out to the text areas.
    jTextArea3.setText(PQ.toString());
    jTextArea4.setText(E.toString());
    jTextArea5.setText(D.toString());
   }
   catch(FileNotFoundException e62) {
       JOptionPane.showMessageDialog(this,
       "File keys.txt not found.");
   }
   catch(IOException e63) {
       JOptionPane.showMessageDialog(this,
       "IO Exception.");
   }

   }

  }



